﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FastSerializer.cs" company="">
//   
// </copyright>
// <summary>
//   The serialization writer.
// </summary>
// --------------------------------------------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;

namespace Systemathics.FrameWork
{
    public class SerializationWriter : BinaryWriter
    {
        private SerializationWriter(Stream s) : base(s)
        {
        }
         
        public static SerializationWriter GetWriter()
        {
            var ms = new MemoryStream();
            return new SerializationWriter(ms);
        }

        public override void Write(byte[] b)
        {
            Write(b.Length);
            base.Write(b);
        }
        public override void Write(char[] c)
        {
            Write(c.Length);
            base.Write(c);
        }
        public void Write(DateTime dt)
        {
            Write(dt.Ticks);
        }
        public void Write(List<TickCapture> c)
        {
            Write(c.Count);
            foreach (TickCapture item in c) Write(item);
        }
        public void Write(IDictionary<DateTime, TickCapture> d)
        {
            try
            {
                if (d != null)
                    if (d.Count > 0)
                    {
                        Write(d.Count);
                        foreach (var kvp in d)
                        {
                            Write(kvp.Key);
                            Write((TickCapture)kvp.Value.Clone());
                        }
                    }
            }
            catch (Exception ee)
            {
                
            }

        }
        public void Write(TickCapture item)
        {
            try
            {
                Write((uint)item.TickInfo);
                Write(item.Value);
            }
            catch (Exception eee)
            {
                
            }

        }
        public void AddToInfo(SerializationInfo info)
        {
            byte[] b = ((MemoryStream) BaseStream).ToArray();
            info.AddValue("X", b, typeof (byte[]));
        }
    }


    public class SerializationReader : BinaryReader
    {
        private SerializationReader(Stream s) : base(s)
        {
        }

        public static SerializationReader GetReader(SerializationInfo info)
        {
            var byteArray = (byte[]) info.GetValue("X", typeof (byte[]));
            var ms = new MemoryStream(byteArray);
            return new SerializationReader(ms);
        }

        public byte[] ReadByteArray()
        {
            int len = ReadInt32();
            if (len > 0) return ReadBytes(len);
            if (len < 0) return null;
            return new byte[0];
        }
        public char[] ReadCharArray()
        {
            int len = ReadInt32();
            if (len > 0) return ReadChars(len);
            if (len < 0) return null;
            return new char[0];
        }
        public DateTime ReadDateTime()
        {
            return new DateTime(ReadInt64());
        }
        public List<TickCapture> ReadList()
        {
            int count = ReadInt32();
            var d = new List<TickCapture>();
            for (int i = 0; i < count; i++) d.Add(ReadTickCapture());
            return d;
        }
        public Dictionary<DateTime, TickCapture> ReadDictionary()
        {
            int count = ReadInt32();
            var d = new Dictionary<DateTime, TickCapture>();
            for (int i = 0; i < count; i++) d[ReadDateTime()] = ReadTickCapture();
            return d;
        }
        public TickCapture ReadTickCapture()
        {
            return new TickCapture((TickInfo) ReadUInt32(), ReadDecimal());
        }
    }
}